package com.sanuo.pico.act.work;

import android.os.Bundle;

import androidx.annotation.Nullable;

import com.sanuo.pico.R;
import com.sanuo.pico.databinding.ActRedBinding;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.event.RedLevel;
import com.sanuo.pico.event.RedVoltage;
import com.sanuo.pico.it.SettingPage;
import com.sanuo.pico.utils.Command;
import com.sanuo.pico.utils.Const;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class RedAct extends BaseWorkActivity implements SettingPage {

    ActRedBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActRedBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(v -> {
            Command.sendTouchEvent(0, 0x20);
            if (Const.jump) {
                finish();
            }
        });
        binding.title.setText(getResources().getStringArray(R.array.settings)[7]);

        setTouch(binding.p1.down, () -> Command.sendTouchEvent(0, 0x94));
        setTouch(binding.p1.up, () -> Command.sendTouchEvent(0, 0x95));

        setTouch(binding.p2.down, () -> Command.sendTouchEvent(0, 0x96));
        setTouch(binding.p2.up, () -> Command.sendTouchEvent(0, 0x97));

        binding.save.setOnClickListener(v -> Command.sendTouchEvent(0, 0x43));

        onMessageEvent(Event.redLevel);
        onMessageEvent(Event.voltage);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RedLevel event) {
        binding.p1.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RedVoltage event) {
        binding.p2.value.setText(String.format("%.2f", event.getValue() / 100f));
    }
}
